package com.hu.gulimallproduct.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hu.gulimallcommon.utils.PageUtils;
import com.hu.gulimallcommon.utils.Query;
import com.hu.gulimallproduct.project.dao.PmsSkuInfoDao;
import com.hu.gulimallproduct.project.entity.PmsSkuInfoEntity;
import com.hu.gulimallproduct.project.service.PmsSkuInfoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

@Service
public class PmsSkuInfoServiceImpl extends ServiceImpl<PmsSkuInfoDao, PmsSkuInfoEntity> implements PmsSkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PmsSkuInfoEntity> queryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if(StringUtils.isNotEmpty(key)){
            if(!"0".equals( key)){
                queryWrapper.and(item->{
                    item.eq("sku_id",key).or().like("sku_name",key);
                });
            }
        }
        String catelogId = (String) params.get("catelogId");
        if(StringUtils.isNotEmpty(catelogId)){
            if(!"0".equals( catelogId)){
                queryWrapper.eq("catalog_id",catelogId);
            }
        }
        String brandId = (String) params.get("brandId");
        if(StringUtils.isNotEmpty(brandId)){
            if(!"0".equals( brandId)){
                queryWrapper.eq("brand_id",brandId);
            }
        }
        String max = (String) params.get("max");
        if(StringUtils.isNotEmpty(max)){
            try {
                BigDecimal bigDecimal = new BigDecimal(max);
                if(bigDecimal.compareTo(new BigDecimal(0))==1){
                    queryWrapper.le("price",max);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        String min = (String) params.get("min");
        if(StringUtils.isNotEmpty(min)){
            queryWrapper.ge("price",min);
        }
        IPage<PmsSkuInfoEntity> page = this.page(
                new Query<PmsSkuInfoEntity>().getPage(params),queryWrapper
        );

        return new PageUtils(page);
    }

}